package cn.javaxc.mvcwork.controller;
import cn.javaxc.mvcwork.controller.SystemStatus;
import java.util.*;
import cn.javaxc.mvcwork.util.common.*;


public class Template {

  public final static int HTMLEncoding = 1;
  public final static int XMLEncoding = 2;

  private HashMap parseElements = null;
  private String text = null;
  private int valueEncoding = -1;


  
  public Template() {
  }


  
  public Template(String theText) {
    text = theText;
  }


  
  public void setValueEncoding(int tmp) {
    this.valueEncoding = tmp;
  }


  
  public int getValueEncoding() {
    return valueEncoding;
  }


  
  public void setParseElements(HashMap tmp) {
    this.parseElements = tmp;
  }


  
  public void setText(String tmp) {
    this.text = tmp;
  }


  
  public HashMap getParseElements() {
    return parseElements;
  }


  
  public String getText() {
    return text;
  }


  
  public String getParsedText() {
    if (parseElements != null) {
      Iterator elements = parseElements.keySet().iterator();
      while (elements.hasNext()) {
        String key = (String) elements.next();
        String value = (String) parseElements.get(key);
        if (valueEncoding == HTMLEncoding) {
          value = StringUtils.toHtmlValue(value);
        } else if (valueEncoding == XMLEncoding) {
          value = XMLUtils.toXMLValue(value);
        }
        text = text.replace( key, value);
      }
    }
    return text;
  }


  
  public void addParseElement(String key, int value) {
    this.addParseElement(key, String.valueOf(value));
  }


  
  public void addParseElement(String key, String value) {
    if (parseElements == null) {
      parseElements = new HashMap();
    }
    if (value != null) {
      parseElements.put(key, value);
    }
  }


  
  public String getValue(String key) {
    String value = null;
    int keyIndex = text.indexOf("${" + key + "=");
    if (keyIndex > -1) {
      StringBuffer parsedValue = new StringBuffer();
      boolean start = false;
      boolean end = false;
      while (keyIndex < text.length() && !end) {
        String val = text.substring(keyIndex, ++keyIndex);
        if (!start && "=".equals(val)) {
          start = true;
        } else {
          if (start) {
            if ("}".equals(val)) {
              end = true;
            } else {
              parsedValue.append(val);
            }
          }
        }
      }
      if (parsedValue.length() > 0) {
        value = parsedValue.toString();
      }
    }
    return value;
  }


  
  public ArrayList getVariables() {
    ArrayList variables = new ArrayList();
    if (text != null) {
      int posCount = 0;
      int loc = -1;
      while ((loc = text.indexOf("${", posCount)) > -1) {
        int endLoc = text.indexOf("}", loc);
        String variable = text.substring(loc + 2, endLoc);
        variables.add(variable);
        posCount = endLoc;
      }
    }
    return variables;
  }


  
  public boolean hasVariable(String input) {
    ArrayList variables = getVariables();
    Iterator i = variables.iterator();
    while (i.hasNext()) {
      String variable = (String) i.next();
      if (variable.equals(input)) {
        return true;
      }
    }
    return false;
  }


  
  public HashMap getKeyValuePairs() {
    HashMap keyValues = new HashMap();
    String key = "";
    String value = "";
    if (text != null) {
      int posCount = 0;
      int loc = -1;
      while ((loc = text.indexOf("${", posCount)) > -1) {
        int endLoc = text.indexOf("}", loc);
        if (text.substring(loc + 2, endLoc).indexOf("=") > 0) {
          key = text.substring(loc + 2, endLoc).split("=")[0];
          value = text.substring(loc + 2, endLoc).split("=")[1];
          keyValues.put(key, value);
        } else {
          keyValues.put(key, "");
        }
        posCount = endLoc;
      }
    }
    return keyValues;
  }


  
  public void populateVariables(Object object) {
    ArrayList variables = getVariables();
    Iterator i = variables.iterator();
    while (i.hasNext()) {
      String variableName = (String) i.next();
      if (System.getProperty("DEBUG") != null) {
        System.out.println("Template-> populateVariables: " + variableName);
      }
      String value = ObjectUtils.getParam(object, variableName);
      if (value == null) {
        value = "";
      }
      addParseElement("${" + variableName + "}", value);
    }
  }


  
  public void populateSystemVariables(SystemStatus thisSystem) {
    ArrayList variables = getVariables();
    Iterator i = variables.iterator();
    while (i.hasNext()) {
      String variableName = (String) i.next();
      String value = thisSystem.getLabel(variableName);
      if (value == null) {
        value = "";
      }
      addParseElement("${" + variableName + "}", value);
    }
  }
}
